<template>
  <div class="app-container">
    <div class="filterDiv">
      <el-input v-model="listQuery.mnemonicCode" size="small" placeholder="摘要代码/名称" style="width: 200px;" class="filter-item" />
      <el-button size="small" type="primary" round @click="getList">查询</el-button>
      <el-button v-permiss="['60207670']" style="float:right" size="small" type="primary" round @click="showImport">导入</el-button>
      <el-button v-permiss="['60207620']" style="float:right" size="small" type="primary" round @click="handleAdd">新增</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="助记码" align="center">
          <template slot-scope="{row}">
            <span>{{ row.mnemonicCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="摘要" align="center">
          <template slot-scope="{row}">
            <span>{{ row.summary }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="{row}">
            <el-button v-permiss="['60207630']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
            <el-button v-permiss="['60207660']" type="text" size="small" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :close-on-click-modal="false" :title="dialogStatus=='create'?'新增摘要':'修改摘要'" :visible.sync="dialogVisible" width="460px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="64px" style="width: 400px; margin-left:10px;">
        <el-form-item label="摘要" prop="summary">
          <el-input v-model="form.summary" placeholder="摘要" />
        </el-form-item>
        <el-form-item label="助记码" prop="mnemonicCode">
          <el-input v-model="form.mnemonicCode" placeholder="助记码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus == 'create'?handleCreate():handleModify()">确定</el-button>
      </div>
    </el-dialog>
    <import-data ref="importRef" type="summary" title="摘要" />
  </div>
</template>
<script>
import { getSummarys, addSummary, delSummary } from '@/api/voucher'
import importData from './importData'
export default {
  name: 'BaseCatogery',
  components: { importData },
  data() {
    return {
      tableKey: 0,
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        mnemonicCode: ''
      },
      form: {
        mnemonicCode: '',
        summary: ''
      },
      dialogVisible: false,
      dialogStatus: '',
      rules: {
        summary: [{ required: true, message: '摘要不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    showImport() {
      this.$refs.importRef.option = {
        show: true
      }
    },
    getList() {
      this.listLoading = true
      getSummarys(this.listQuery).then(res => {
        this.listLoading = false
        this.tableData = res.data.data || []
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleAdd(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'create'
      this.form.id = ''
      this.form.mnemonicCode = ''
      this.form.summary = ''
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'update'
      this.form.id = obj.id
      this.form.mnemonicCode = obj.mnemonicCode
      this.form.summary = obj.summary
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDel(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItem(id)
      })
    },
    handleModify() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addSummary(this.form).then(res => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    handleCreate() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addSummary(this.form).then(res => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    delItem(id) {
      delSummary(id).then(res => {
        if (res.data.errorCode === '0') {
          this.getList()
          this.dialogVisible = false
          this.$message.success(res.data.msg)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.fileDiv {
  width: 400px;
  height: 60px;
  border: 1px #d0d0d0 solid;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
}
.filter-item {
  position: relative;
}
/deep/.el-form-item {
  margin-bottom: 18px;
}
</style>
